/*
 * GenNum - an ugly way to grab each day's game number from a cache
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

// to use this you have to build a "gamedata" textfile with space-seperated
// integers from 0 to 1000000. Then run the server with the writeserialized
// call uncommented.

// I used quantum random bit generator to make the spacecreeps.com
// dataset.

class GenNum
{

	public static var data : Array<Int>;
	static var startdate = Date.fromString("2007-12-04 00:00:00");
	static var dayinms = 1000*60*60*24;
	
	static public function todaysgame(date : Date) : Int
	{
		var starttime = startdate.getTime();
		var curtime = date.getTime();
		var elapsed = curtime - starttime;
		var numtoget = Std.int(elapsed/dayinms);
		return data[numtoget];
	}
	
	static function writeserialized()
	{
		var f = neko.io.File.read("sortednumbers",false);
		var rawstr = f.readAll();
		f.close();
		var data = rawstr.split(" ");
		var newdata : Array<Int> = new Array();
		for (n in data)
			newdata.push(Std.parseInt(n));
		var ff = neko.io.File.write("gamedata",false);
		ff.write(haxe.Serializer.run(newdata));
		ff.close();
	}

	static public function loadcache()
	{
		trace("getting game number cache");
		var f = neko.io.File.read("gamedata",false);
		var rawstr = f.readAll();
		f.close();

		data = haxe.Unserializer.run(rawstr);		
		rawstr = null;	
		trace("cache loaded");
	}
	
	static public function main()
	{
	
		//writeserialized(); // only need to do this with new datasets

		loadcache();	
	}
	
}
